package online.xybh.qa.service.Impl;

import online.xybh.qa.dao.AllClassMapper;
import online.xybh.qa.dao.TeacherMapper;
import online.xybh.qa.dto.TeacherAndClassDTO;
import online.xybh.qa.model.AllClass;
import online.xybh.qa.model.AllClassExample;
import online.xybh.qa.model.Teacher;
import online.xybh.qa.service.TeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: xybh
 * @Description:
 * @Date: Created in 22:08 2020/10/21
 * @Modified:
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private AllClassMapper classMapper;

    @Override
    public Teacher queryTeacherById(String teacherId) {
        Teacher teacher = teacherMapper.selectByPrimaryKey(teacherId);
        return teacher;
    }

    @Override
    public TeacherAndClassDTO queryTeacherAndClassById(String teacherId, String password) {
        Teacher teacher = teacherMapper.selectByPrimaryKey(teacherId);
        TeacherAndClassDTO res = new TeacherAndClassDTO();
        if (teacher != null && password.equals(teacher.getPassword())) {
            AllClassExample allClassExample = new AllClassExample();
            allClassExample.createCriteria()
                    .andTeacherEqualTo(teacher.getTeacherId());
            List<AllClass> allClasses = classMapper.selectByExample(allClassExample);
            res.setTeacherId(teacher.getTeacherId());
            res.setName(teacher.getName());
            res.setSubject(teacher.getSubject());
            res.setTitle(teacher.getTitle());
            ArrayList<String> classNames = new ArrayList<>();
            for (AllClass c :
                    allClasses) {
                classNames.add(c.getClassId());
            }
            res.setClassName(classNames);
            Teacher lastLogin = new Teacher();
            BeanUtils.copyProperties(teacher, lastLogin);
            lastLogin.setLastLoginTime(new Date());
            teacherMapper.updateByPrimaryKey(lastLogin);
        }
        return res;
    }

}
